package XMLread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;

import database.MapDbAdapter;

/**
 * 
 * Parses a CSV file that contains definitions of CSVbuildings
 *
 */
public class ParseCSVbuilding {
	
	/**
	 * Adds all buildings contained in csv file into database stored within the application
	 * @param buildingCSV the input stream obtained by opening the CSV file located in the assets folder
	 * @mapDbAdapter the database of this application
	 */
	public static void parse(InputStream buildingCSV, MapDbAdapter mapDbAdapter){
		
		//create BufferedReader to read csv file
		BufferedReader br;
		try {
			mapDbAdapter.open();
			br = new BufferedReader(new InputStreamReader(buildingCSV, "UTF-8"));	
			String strLine = br.readLine();
			StringTokenizer st = null;
			
			//create each space and add it to hashMap accordingly
			while( (strLine = br.readLine()) != null)
			{
				//break comma separated line using ","
				st = new StringTokenizer(strLine, ",");
				//create the building
				String shape = st.nextToken();
				String name = st.nextToken();
				String short_name = st.nextToken();
				String buildingid = st.nextToken();
				mapDbAdapter.addBuilding(buildingid, name, short_name, shape);
			}
			mapDbAdapter.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
